//
//  RankShotsController.m
//  CoolTennisBall
//
//  Created by Coollang on 2017/7/28.
//  Copyright © 2017年 CoolLang. All rights reserved.
//

#import "RankShotsController.h"
#import "RanktablevieCell.h"
#import "RankingModel.h"
#import "RankUserInfoController.h"

@interface RankShotsController ()

@property (strong, nonatomic) IBOutlet UITableView *scoreTableView;

@end

@implementation RankShotsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:RanktablevieCellIdentifierID bundle:nil] forCellReuseIdentifier:RanktablevieCellIdentifierID];
    self.tableView.rowHeight = kAutoHei(59.0f);
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)setDataSource:(NSArray<RankingModel *> *)dataSource {
    _dataSource = dataSource;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RanktablevieCell *cell = [tableView dequeueReusableCellWithIdentifier:RanktablevieCellIdentifierID forIndexPath:indexPath];
    
    RankingModel *rankModel = self.dataSource[indexPath.row];
    [cell configureCompetitionCellByModel:rankModel indexPath:indexPath rankModeType:RankModeTypeSwings];
    
    [cell setCellLikeHandelBlock:^(LikeCallBackBlock callBack){
        if (rankModel.beingRequest == NO && self.likeClickHandleBlock){
            rankModel.beingRequest = YES;
            self.likeClickHandleBlock(rankModel, ^(BOOL success) {
                rankModel.beingRequest = NO;
                if (success){
                    RankingModel *rankModel = self.dataSource[indexPath.row];
                    [self.dataSource[indexPath.row] setIsLiked:!(rankModel.isLiked)];
                    NSInteger change = !(rankModel.isLiked) ? 1:-1;
                    [self.dataSource[indexPath.row] setLikes:@(rankModel.likes.integerValue + change).stringValue];
                }
                callBack(success);
            });
        }else {
            callBack(NO);
        }
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.showUserRankInfoVcBlock){
        self.showUserRankInfoVcBlock(self.dataSource[indexPath.row], ^(BOOL success) {
            if (success){
                RankingModel *rankModel = self.dataSource[indexPath.row];
                [self.dataSource[indexPath.row] setIsLiked:!(rankModel.isLiked)];
                NSInteger change = (rankModel.isLiked) ? 1:-1;
                [self.dataSource[indexPath.row] setLikes:@(rankModel.likes.integerValue + change).stringValue];
                [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        });
    }
    
}

@end
